/*
 * This file is part of the Boring Engine's Kirby Game.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package kirby;

import java.util.ArrayList;
import java.util.Iterator;

public class CollisionDetection {

    public static final int CANT_MOVE = 0,
            MOVE_X = 1,
            MOVE_Y = 2,
            MOVE_X_AND_Y = 3;

    private static boolean[][] solidmap;

    public static void setSolidMap(boolean[][] map) {
        solidmap = map;
    }

    public static int canMoveToX(int x, int y, int w, int h, int dx) {
        if (x + dx < 0) return CANT_MOVE;
        if (x + dx + w > solidmap[0].length * 16) return CANT_MOVE;
        if (y < 0) return CANT_MOVE;
        if (y + h > solidmap.length * 16) return CANT_MOVE;
        int kx = (x+dx) / 16, ky = y / 16, kx2 = (x+dx+w-1) / 16, ky2 = (y+h-1) / 16;
        for (int i = kx; i <= kx2; i++)
            for (int j = ky; j <= ky2; j++)
                if (solidmap[j][i])
                    return CANT_MOVE;
        return MOVE_X;
    }

    public static int canMoveToY(int x, int y, int w, int h, int dy) {
        if (x < 0) return CANT_MOVE;
        if (x + w > solidmap[0].length * 16) return CANT_MOVE;
        if (y + dy < 0) return CANT_MOVE;
        if (y + dy + h > solidmap.length * 16) return MOVE_Y;
        int kx = x / 16, ky = (y+dy) / 16, kx2 = (x+w-1) / 16, ky2 = (y+dy+h-1) / 16;
        for (int i = kx; i <= kx2; i++)
            for (int j = ky; j <= ky2; j++)
                if (solidmap[j][i])
                    return CANT_MOVE;
        return MOVE_Y;
    }

    public static void checkCollide(ArrayList<GameObject> objs, Kirby kirby, ArrayList<Projectile> projectiles) {
        int kx = kirby.getX(), ky = kirby.getY(), kw = kirby.getWidth(), kh = kirby.getHeight();
        for (GameObject o : objs) {
            int ox = o.getX(), oy = o.getY(), ow = o.getWidth(), oh = o.getHeight();
            if (ox + ow < kx) continue;
            if (ox > kx + kw) continue;
            if (oy + oh < ky) continue;
            if (oy > ky + kh) continue;
            kirby.doCollide(o);
            o.doCollide(kirby);
        }
        Iterator<Projectile> iter = projectiles.iterator();
        while (iter.hasNext()) {
            Projectile projectile = iter.next();
            int px = projectile.getX(), py = projectile.getY(), pw = projectile.getWidth(),
                    ph = projectile.getHeight();
            for (GameObject o : objs) {
                int ox = o.getX(), oy = o.getY(), ow = o.getWidth(), oh = o.getHeight();
                if (ox + ow < px) continue;
                if (ox > px + pw) continue;
                if (oy + oh < py) continue;
                if (oy > py + ph) continue;
                projectile.doCollide(o);
                o.doCollide(projectile);
            }
            if (projectile.isDead())
                iter.remove();
        }
    }

}
